package com.camel.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.base.model.ZsSurvey;
import com.camel.base.model.ZsWorkRecord;
import com.camel.base.model.ZsWorkShift;
import com.camel.base.service.ZsSurveyService;
import com.camel.base.service.ZsWorkRecordService;
import com.camel.base.service.ZsWorkShiftService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.enums.ResultEnum;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author camel
 */
@RestController
@RequestMapping("/zsWorkRecord")
public class ZsWorkRecordController extends BaseCommonController<ZsWorkRecord> {

    @Autowired
    private ZsWorkRecordService<ZsWorkRecord> service;

    @Autowired
    private ZsWorkShiftService workShiftService;

    @Autowired
    private ZsSurveyService<ZsSurvey> surveyService;

    /**
     * 验证我是否能开始某个问卷的调查
     * @param id
     * @return
     */
    @GetMapping("/valid/{id}")
    private Result validIStart(@PathVariable Integer id) throws ParseException {
        // 今日日期
        String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        Date startDate = DateUtils.parseDate(format + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(format + " 23:59:59", "yyyy-MM-dd HH:mm:ss");

        // 今天的班次
        List<ZsWorkShift> workShifts = workShiftService.list(new QueryWrapper<ZsWorkShift>()
                .eq("survey_id", id)
                .ge("shift_date", startDate)
                .lt("shift_date", endDate));
        if(CollectionUtils.isEmpty(workShifts)) {
            throw new CustomException("当前无班次,请关注首页班次信息申请后再试");
        }
        Date n = new Date();
        for (ZsWorkShift workShift : workShifts) {
            String startTimeStr = format + " " + workShift.getStartTime();
            String endTimeStr = format + " " + workShift.getEndTime();
            try {
                Date startTime = DateUtils.parseDate(startTimeStr, "yyyy-MM-dd HH:mm");
                Date endTime = DateUtils.parseDate(endTimeStr, "yyyy-MM-dd HH:mm");
                if(n.getTime() > startTime.getTime() && n.getTime() < endTime.getTime()) {
                    return ResultUtil.success("您已经申请并通过班次了");
                } else {
                    continue;
                }
            } catch (ParseException e) {
                System.err.println(e.getMessage());
                // 数据库班次数据异常
                continue;
            }
        }
        return ResultUtil.error(ResultEnum.BAD_REQUEST.getCode(), "当前无班次,请关注首页班次信息申请后再试");
    }

    @Override
    public boolean deleteAble(Serializable id) {
        ZsWorkRecord byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    @Override
    public BaseCommonService<ZsWorkRecord> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "班次报名";
    }
}
